package com.example.demo.dao;

import com.example.demo.config.error.BizException;
import com.example.demo.entity.Department;
import com.example.demo.entity.Doctor;
import com.example.demo.mapper.DepartmentMapper;
import com.example.demo.mapper.DoctorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: WinstonDeng
 * @Description:
 * @Date: Created in 9:42 2020/4/9
 * @Modified by:
 */
@Repository
public class DoctorDao {
    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    public Doctor getById(long id) throws BizException {
        Doctor doctor = doctorMapper.findById(id);
        if (doctor == null){
            throw new BizException("404", "未找到该doctor");
        }else {
            Department department = departmentMapper.findById(doctor.getDepartmentId());
            if(department == null){
                throw new BizException("404", "未找到该department");
            }
            doctor.setDepartment(department);
            return doctor;
        }
    }

    public Doctor getByUsername(String username) throws BizException{
        // 默认 username 唯一
        Doctor doctor = doctorMapper.findByUsername(username);
        if (doctor == null) {
            throw new BizException("404", "未找到该username");
        }
        Department department = departmentMapper.findById(doctor.getDepartmentId());
        if(department == null){
            throw new BizException("404", "未找到该department");
        }
        doctor.setDepartment(department);
        return doctor;
    }

    public List<Doctor> getListByName(String doctorName) throws BizException{
        List<Doctor> doctorList = doctorMapper.findByRealName(doctorName);
        for(Doctor doctor:doctorList){
            Department department = departmentMapper.findById(doctor.getDepartmentId());
            if(department == null){
                throw new BizException("404", "未找到该department");
            }
            doctor.setDepartment(department);
        }
        return doctorList;
    }

    public List<Doctor> getListByDepartmentName(String departmentName) throws BizException{
        Department department = departmentMapper.findByName(departmentName);
        if (department == null){
            throw new BizException("404", "未找到 department");
        }
        List<Doctor> doctorList = doctorMapper.findByDepartmentId(department.getId());
        for(Doctor doctor:doctorList){
            doctor.setDepartment(department);
        }
        return doctorList;
    }

    public Doctor getByOpenId(String openId) {
        Doctor doctor = doctorMapper.findByOpenId(openId);
        if (doctor!=null){
            Department department = departmentMapper.findById(doctor.getDepartmentId());
            doctor.setDepartment(department);
        }
        return doctor;
    }

    public Doctor updateDoctor(Doctor doctor) throws BizException{
        boolean flag = doctorMapper.updateDoctor(doctor);
        if(!flag){
            throw new BizException("500", "update error");
        }
        return doctor;
    }
}
